/**
 * 
 */
package Exercise;

/**
 * @author zhang
 *
 */
public class BulbZTE {

	public static void main(String[] args) {
		// int[] status = { 1, 0, 1, 1, 0, 1, 1 };
		// int[] distance = { 1, 5, 6, 7, 8, 9, 17 };
		int[] status = { 0, 1, 0, 0, 1, 1, 0, 0 };
		int[] distance = { 3, 5, 10, 12, 13, 23, 30, 38 };
		System.out.println(minValue(8, status, distance));

	}

	public static int minValue(int numOfBulbs, int[] glowState, int[] distance) {
		int max = 0, min, index, j;
		for (int i = 0; i < numOfBulbs; i++) {
			if (glowState[i] == 1) {
				++i;
			}
			min = Integer.MAX_VALUE;
			index = -1;
			for (int k = 0; k < numOfBulbs; k++) {
				j = k + 1;
				while (glowState[k] == 0 && j < numOfBulbs) {
					if (glowState[j] == 1) {
						if (distance[j] - distance[k] < min) {
							min = distance[j] - distance[k];
							index = k;
						}
						break;
					}
					++j;
				}
				j = k - 1;
				while (glowState[k] == 0 && j > 0) {
					if (glowState[j] == 1) {
						if (distance[k] - distance[j] < min) {
							min = distance[k] - distance[j];
							index = k;
						}
						break;
					}
					--j;
				}
			}
			if (min != Integer.MAX_VALUE) {
				max += min;
				glowState[index] = 1;
			}
		}
		return max;
	}

}
